﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using CustomBundleConfiguration = OptimizationDemo.Optimization.Configuration.BundleConfiguration.Bundles;
using BundleSerializer = OptimizationDemo.Optimization.Configuration.Deserializer;
namespace OptimizationDemo.Optimization
{
    public class CustomOptimizationWrapper
    {
        #region Private Declarations
        private HttpContextBase currentContext { get; set; }
        private BundleContext cssContext { get; set; }
        private BundleContext jsContext { get; set; }
        private BundleContext lessContext { get; set; }
        private Bundle cssBundle { get; set; }
        private Bundle jsBundle { get; set; }
        private Bundle lessBundle { get; set; }
        #endregion

        public void Configure(BundleCollection collection)
        {
            #region Initialize Components

            currentContext = new HttpContextWrapper(HttpContext.Current);
            CustomBundleConfiguration customBundles = new CustomBundleConfiguration();
            customBundles = BundleSerializer.Serializer();

            #endregion

            if (!customBundles.IsEnabled)
                return;

            //Here in this example i'm just showing how to do content managed asset bundling this with one asset
            //Write for loop and generate custom bundle class list to have multiple items bundled

            if (customBundles.Bundle.BundleAssetType.ToLower() == "css" && customBundles.Bundle.BundleType.ToLower() == "folder")
            {
                //Create a new bundle context
                cssContext = new BundleContext(currentContext, collection, customBundles.Bundle.OutputPath);
                //Create a new bundle
                cssBundle = new Bundle(customBundles.Bundle.OutputPath, new CssMinify());
                //Create a custom orderer / default oreder for the bundle
                cssBundle.Orderer = new DefaultBundleOrderer();
                //Include items for current bundle
                cssBundle.IncludeDirectory(customBundles.Bundle.Assets.Asset.AssetPath, "*.css", true);
                //Invoke the orderer to sort the files
                cssBundle.Orderer.OrderFiles(cssContext, cssBundle.EnumerateFiles(cssContext));
                //Insert the new bundle to bundle table
                collection.Add(cssBundle);
            }
            else if (customBundles.Bundle.BundleAssetType.ToLower() == "js" && customBundles.Bundle.BundleType.ToLower() == "folder")
            {
                jsContext = new BundleContext(currentContext, collection, customBundles.Bundle.OutputPath);
                jsBundle = new Bundle(customBundles.Bundle.OutputPath, new JsMinify());
                jsBundle.Orderer = new DefaultBundleOrderer();
                jsBundle.IncludeDirectory(customBundles.Bundle.Assets.Asset.AssetPath, "*.js", true);
                jsBundle.Orderer.OrderFiles(jsContext, cssBundle.EnumerateFiles(jsContext));
                collection.Add(jsBundle);
            }
            else if (customBundles.Bundle.BundleAssetType.ToLower() == "less" && customBundles.Bundle.BundleType.ToLower() == "folder")
            {
                //private BundleContext lessContext { get; set; }
                //private Bundle cssBundle { get; set; }
                // Above variables are declared as private scope for the class

                //Create a new bundle context
                lessContext = new BundleContext(currentContext, collection, customBundles.Bundle.OutputPath);
                //Create a new bundle
                lessBundle = new Bundle(customBundles.Bundle.OutputPath, new OptimizationDemo.Optimization.CustomPipeline.LessCompiler());
                //Create a custom orderer / default oreder for the bundle
                lessBundle.Orderer = new OptimizationDemo.Optimization.CustomPipeline.LessOrderer();
                //Include items for current bundle
                lessBundle.IncludeDirectory(customBundles.Bundle.Assets.Asset.AssetPath, "*.css", true);
                //Invoke the orderer to sort the files
                lessBundle.Orderer.OrderFiles(cssContext, cssBundle.EnumerateFiles(cssContext));
                //Insert the new bundle to bundle table
                collection.Add(lessBundle);
            }

        }
    }
}